/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufc.quixada.comandos;

import br.com.ufc.quixada.entidades.Clientes;
import br.com.ufc.quixada.interfaces.Comando;
import br.com.ufc.quixada.service.ClienteService;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Fuscao
 */
public class CmdLogin implements Comando {

    @Override
    public String executa(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        String RESULTADO = (String) session.getAttribute("RESULTADO");
        
        String email = request.getParameter("email");
        String senha = request.getParameter("senha");
        
        Clientes cliente = new Clientes();
        ClienteService cs = null;
        try {
            cs = new ClienteService();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CmdLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        cliente = cs.clientefindByEmail(email);
        
        if(cliente != null && senha.trim().equals(cliente.getSenha())){
            if(cliente.isValido()){
                session.setAttribute("user", cliente);
                session.setAttribute("sucesso", "cliente Logado com Sucesso!!");
                return "/home.jsp";
            }else{
                session.setAttribute("erro", "Cadastrar não ativado - Ative sua conta por email!!");
                return "/login.jsp";
            }
        }else{
            session.setAttribute("erro", "Dados incorretos ou inexistentes!!");
            return "/login.jsp";
        }
        
        
    }


    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
}
